package com.qys.livesMall.app.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.qys.livesMall.app.user.model.entity.IpOperateStatistics;
import com.qys.livesMall.app.user.mapper.IpOperateStatisticsMapper;
import com.qys.livesMall.app.user.service.IpOperateStatisticsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2023-11-08
 */
@Service
public class IpOperateStatisticsServiceImpl extends ServiceImpl<IpOperateStatisticsMapper, IpOperateStatistics> implements IpOperateStatisticsService {

    @Override
    public boolean add(String ip, Integer type) {
        LambdaQueryWrapper<IpOperateStatistics> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(IpOperateStatistics::getIp,ip);
        queryWrapper.eq(IpOperateStatistics::getType,type);
        List<IpOperateStatistics> list = list(queryWrapper);
        if (CollectionUtils.isEmpty(list)){
            IpOperateStatistics ipOperateStatistics=new IpOperateStatistics();
            ipOperateStatistics.setNum(1);
            ipOperateStatistics.setType(type);
            ipOperateStatistics.setIp(ip);
            save(ipOperateStatistics);
        }else {
            IpOperateStatistics ipOperateStatistics=list.get(0);
            ipOperateStatistics.setNum(ipOperateStatistics.getNum()+1);
            baseMapper.updateById(ipOperateStatistics);
        }
        return true;
    }

    @Override
    public List<IpOperateStatistics> getbyIps(List<String> ips,Integer type) {
        LambdaQueryWrapper<IpOperateStatistics> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(IpOperateStatistics::getIp,ips);
        queryWrapper.eq(IpOperateStatistics::getType,0);
        return list(queryWrapper);
    }
}
